package dnl.util.ui;

import java.awt.AWTEventMulticaster;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Clicker implements MouseListener {
	private Component comp = null;

	private ActionListener actionListener = null;

	private String command = null;

	private boolean mouseOver = false;

	//private boolean mouseDown = false;

	public Clicker(Component c, String cmd) {
		this.comp = c;
		this.comp.addMouseListener(this);
		setActionCommand(cmd);
	}
	@Override
	public void mouseClicked(MouseEvent me) {
	}
	@Override
	public void mouseEntered(MouseEvent me) {
		this.mouseOver = true;
	}
	@Override
	public void mouseExited(MouseEvent me) {
		this.mouseOver = false;
	}
	@Override
	public void mousePressed(MouseEvent me) {
		//this.mouseDown = true;
	}
	@Override
	public void mouseReleased(MouseEvent me) {
		//this.mouseDown = false;

		if (this.mouseOver) // only fire an event if released while over
		{
			fireAction();
		}
	}

	public String getActionCommand() {
		return this.command;
	}

	public void setActionCommand(String cmd) {
		this.command = cmd;
	}

	public void addActionListener(ActionListener l) {
		this.actionListener = AWTEventMulticaster.add(this.actionListener, l);
	}

	public void removeActionListener(ActionListener l) {
		this.actionListener = AWTEventMulticaster
				.remove(this.actionListener, l);
	}

	protected void fireAction() {
		if (actionListener != null) {
			actionListener.actionPerformed(new ActionEvent(this.comp,
					ActionEvent.ACTION_PERFORMED, this.command, 0));
		}
	}
}
